package com.desmart.renjiao.announcement;

import android.util.Log;

import com.desmart.renjiao.bean.Announcement;
import com.desmart.renjiao.bean.LoginBean;
import com.desmart.renjiao.http.ApiUrl;
import com.desmart.renjiao.xmlparase.AnnouncmentXmlUtils;
import com.google.gson.Gson;
import com.lemon.library.constants.PreferencesConstants;
import com.lemon.library.net.rx.RxObservableListener;
import com.lemon.library.utils.PreferencesUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class AnnouncementListP extends AnnouncementList.AnnouncementListP{

    @Override
    void getAnnouncement(int pageNum, String isCom) {

        String username = "";
        String loginInfoStr = PreferencesUtils.getString(PreferencesConstants.login_info);
        if (loginInfoStr != null) {
            LoginBean loginBean = new Gson().fromJson(loginInfoStr, LoginBean.class);
            username = loginBean.getName();
        }

        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\"><soapenv:Header/><soapenv:Body><urn:notice>" +
                "<title></title>" +
                "<type></type>" +
                "<category></category>" +
                "<level></level>" +
                "<company></company>" +
                "<releaseTime></releaseTime>" +
                "<author></author>" +
                "<isTop></isTop>" +
                "<link></link>" +
                "<username>" + username +
                "</username>" +
                "<pageSize>20</pageSize>" +
                "<pageNo>" + pageNum +
                "</pageNo>" +
                "<totalPage></totalPage>" +
                "<totalDocs></totalDocs>" +
                "<query></query>" +
                "<isCom>" + isCom +
                "</isCom></urn:notice></soapenv:Body>" +
                "</soapenv:Envelope>";

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);


        getRxManager().addObserver(mModel.getTheNotice(ApiUrl.theNotice, body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                String ss = null;
                try {
                    ss = responseBody.string();
                    Log.e("Hqq",ss);
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    List<Announcement> list = AnnouncmentXmlUtils.readXML(inputStream);
                    mView.getListSuccess(list);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    void getTheNews(int pageNum, String LoginName) {

        String username = "";
        String loginInfoStr = PreferencesUtils.getString(PreferencesConstants.login_info);
        if (loginInfoStr != null) {
            LoginBean loginBean = new Gson().fromJson(loginInfoStr, LoginBean.class);
            username = loginBean.getName();
        }

        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\"><soapenv:Header/><soapenv:Body><urn:news>" +
                "<title></title>" +
                "<type></type>" +
                "<category></category>" +
                "<level></level>" +
                "<company></company>" +
                "<releaseTime></releaseTime>" +
                "<author></author>" +
                "<isTop></isTop>" +
                "<link></link>" +
                "<username>" + username +
                "</username>" +
                "<pageSize>20</pageSize>" +
                "<pageNo>" + pageNum +
                "</pageNo>" +
                "<totalPage></totalPage>" +
                "<totalDocs></totalDocs>" +
                "<query></query>" +
                "</urn:news></soapenv:Body>" +
                "</soapenv:Envelope>";

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);

        getRxManager().addObserver(mModel.getTheNews(ApiUrl.theNews, body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                String ss = null;
                try {
                    ss = responseBody.string();
                    Log.e("Hqq",ss);
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    List<Announcement> list = AnnouncmentXmlUtils.readXMLtheNews(inputStream);
                    mView.getListSuccess(list);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }


}
